<template>
  <!-- // 分配角色 -->
  <div>
    这里将来会放置多选列表
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hAddRoles()">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    roleid: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []//
    }
  },
  created() {
    this.loadgetRoles()
  },
  methods: {
    //
    async hAddRoles() {
      try {
        const res = await assignRoles({ id: this.roleid, roleIds: this.roleIds })
        if (res.code !== 10000) return
        this.$message.success(res.message)
        this.loadgetRoles()
        this.$emit('close')
      } catch (error) {
        console.log(error
        )
      }
    },
    // 获取部门列表
    async loadgetRoles() {
      try {
        const res = await getRoles()
        console.log('getRoles', res)

        // 获取已经
        const info = await getUserDetailById(this.roleid)
        console.log('info', info)
        this.roleIds = info.data.roleIds || []// 判断
        this.list = res.data.rows
        // 保存数据
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    //
    closeDialog() {

    }
  }
}
</script>
